using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SystemHotKeysLib;

namespace SystemHotKeysDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private SystemHotKeyListener _hotKeyListner = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            // Initialize the SystemHotKeyListener.
            _hotKeyListner = new SystemHotKeyListener();

            // Attach the event processing procedure.
            _hotKeyListner.SystemHotKeyPressed +=
                new SystemHotKeyPressedEventHandler(_hotKeyListner_SystemHotKeyPressed);
            _hotKeyListner.SystemHotKeyDoublePressed +=
                new SystemHotKeyPressedEventHandler(_hotKeyListner_SystemHotKeyDoublePressed);

            // Create a hot key (Windows Logo key + Esc)
            HotKey testHotKey = new HotKey("TEST", KeyModifiers.Windows, Keys.Escape);

            // Register this hot key.
            _hotKeyListner.Register(testHotKey);
        }

        private void _hotKeyListner_SystemHotKeyPressed(object sender, SystemHotKeyPressedEventArgs e)
        {
            switch (e.HotKeyName)
            {
                case "TEST":
                    RestoreForm();
                    label1.Text = "The Windows Logo + Esc key was pressed.";
                    break;
            }
        }

        private void _hotKeyListner_SystemHotKeyDoublePressed(object sender, SystemHotKeyPressedEventArgs e)
        {
            switch (e.HotKeyName)
            {
                case "TEST":
                    RestoreForm();
                    label1.Text = "The Windows Logo + Esc key was double pressed.";
                    break;
            }
        }

        private void RestoreForm()
        {
            if (this.Visible == false)
            {
                this.Visible = true;
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
            }
            this.Activate();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
                this.ShowInTaskbar = false;
            }
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }
    }
}